package org.samith.web.helper;


import org.samith.dao.AdminUserDao;
import org.samith.entity.AdminUser;

public class AdminUserSessionManager extends AbstractUserSessionManager {
	
	private AdminUserDao adminUserDao;
	
	public AdminUserSessionManager(AdminUserDao adminUserDao){
		this.adminUserDao = adminUserDao;
	}
	
	@Override
	public AuthenticatedUserSession processRequest(LoginRequest request) {
		AdminUser adminUser = adminUserDao.findByUserName(request.getUserName());
		if(adminUser != null){
			if(adminUser.getPassword().equals(DigestAlgorithm.getSHA1Digest(request.getPassword()))){
				AuthenticatedUserSession userSession = new AuthenticatedUserSession(true);
				userSession.setAuthenticated(true);
				userSession.setFirstName(adminUser.getUserName());
				userSession.setUserLevel(0);
				userSession.setUserId(adminUser.getId());
				return userSession;
			}
		}
		return null;
	}
}